var fn = require('../../util/util.js');
var mUrl = getApp();
Page({
  data: {
    inTheaters: {},
    comingSoon: {},
    top: {},
    conPanelShow: true,
    searchPanelShow: false
  },
  // 接口类型
  // RESFul API JSON，轻量级
  // SOAP XML WSDL代理类
  onLoad: function () {
    // 发起网络请求,访问第三方数据API
    // 其他版本开发工具，"Content-Type"对应的值为"application/json"或" "空字符串都不可用，可尝试改为"application/xml"或者"json"
    var inTheatersUrl = mUrl.globalData.m_url + '/v2/movie/in_theaters?start=0&count=3';
    var comingSoonUrl = mUrl.globalData.m_url + '/v2/movie/coming_soon?start=0&count=3';
    var topUrl = mUrl.globalData.m_url + '/v2/movie/top250?start=0&count=3';
    fn.http(inTheatersUrl, this.getMoviesData, 'inTheaters', '正在热映');
    fn.http(comingSoonUrl, this.getMoviesData, 'comingSoon', '即将上映');
    fn.http(topUrl, this.getMoviesData, 'top', 'top250');
  },
  // 搜索
  onBindFoucs: function () {
    this.setData({
      conPanelShow: false,
      searchPanelShow: true
    });
  },
  closeSearchPanel: function () {
    this.setData({
      conPanelShow: true,
      searchPanelShow: false
    });
  },
  onBindConfirm: function (even) {
    var sUrl = mUrl.globalData.m_url + '/v2/movie/search?q={' + even.detail.value + '}';
    fn.http(sUrl, this.getMoviesData, 'searchMovies', 'searchMovies');
  },
  // 处理电影数据
  getMoviesData: function (data, key, name) {
    // this 在此处永远指向当前页面对象
    var infoArr = data.subjects,
      moviesData = [];
    for (var i = 0; i < infoArr.length; i++) {
      var obj = {};
      obj['id'] = infoArr[i]['id'];
      obj['img'] = infoArr[i]['images']['large'];
      obj['title'] = infoArr[i]['title'].length > 6 ? infoArr[i]['title'].substr(0, 6) + '...' : infoArr[i]['title'];
      obj['score'] = infoArr[i]['rating']['average'];
      obj['star'] = fn.getStarArr(infoArr[i]['rating']['stars']);
      moviesData.push(obj);
    }
    var temp = {};
    temp[key] = { 'movieType': name, 'movieList': moviesData };
    // this.setData，中的键不能传参
    this.setData(temp);
  },

  getMoreMoviesBtn: function (even) {
    // 传递参数的方法：
    // 方法一：利用缓存
    // 方法二：利用url
    // 方法三：全局变量
    // 方法四：利用如AnglarJs的发射事件和接受事件
    var category = even.currentTarget.dataset.category;
    wx.navigateTo({
      url: '/page/movies/more-movies/more-movies?category=' + category,
    });
  },
  onOpenMovieDetal: function (even){
    var id = even.currentTarget.dataset.movieId;
    wx.navigateTo({
      url: '/page/movies/movie-detail/movie-detail?id=' + id,
    });
  },
  
  
})